#include "StdAfx.h"
#include "DidsonIni.h"
#include <fstream>

using namespace std;

CDidsonIni::CDidsonIni(void)
{
}

CDidsonIni::~CDidsonIni(void)
{
}

BOOL	CDidsonIni::SaveDidsonIni(TCHAR* inifilename )
{
	TCHAR* strfilename;
	if( inifilename == NULL )
	{
		TCHAR Buffer[MAX_PATH];	
		GetModuleFileName(  NULL, Buffer, sizeof(Buffer) );
		int nstrpos = _tcsclen( Buffer );
		while( *(Buffer + nstrpos--) != L'\\' );	
		Buffer[nstrpos+1]	= L'\0';
		_tcscat(Buffer, L"\\DidsonV5.ini");
		strfilename = Buffer;
	}
	else
	{

		strfilename = inifilename;
	}
	//char buf[100];
	fstream inifile( strfilename, fstream::binary|fstream::out );
	if( !inifile ) return FALSE;

	inifile.write( reinterpret_cast<char*>(m_cBuffhead), 17  );
	inifile.write( reinterpret_cast<char*>(&m_nMount), 4  );

	inifile.write( reinterpret_cast<char*>(&m_nPort), 4  );
	inifile.write( reinterpret_cast<char*>(&m_nSspd), 4  );
	inifile.write( reinterpret_cast<char*>(&m_nTimeout), 4  );
	inifile.write( reinterpret_cast<char*>(&m_nSonarRecordMode), 4  );
	inifile.write( reinterpret_cast<char*>(&m_nSonarFramesPerFile), 4  );
	inifile.write( reinterpret_cast<char*>(&m_nSonarFrameRate), 4  );
	inifile.write( reinterpret_cast<char*>(m_cIPaddress), 16  );
	inifile.write( reinterpret_cast<char*>(m_cNetmask), 16  );
	inifile.write( reinterpret_cast<char*>(m_cGateway), 16  );

	inifile.write( reinterpret_cast<char*>(&m_nSN), 4  );
	inifile.write( reinterpret_cast<char*>(&m_nMclk), 4  );
	inifile.write( reinterpret_cast<char*>(&m_nWaterTemp), 4  );
	inifile.write( reinterpret_cast<char*>(&m_nSalinity), 4  );
	inifile.write( reinterpret_cast<char*>(&m_nFocusMode), 4  );

	inifile.write( reinterpret_cast<char*>(&m_bLongRange), 4  );
	inifile.write( reinterpret_cast<char*>(&m_bPrism), 4  );
	inifile.write( reinterpret_cast<char*>(&m_b3000m), 4  );
	inifile.write( reinterpret_cast<char*>(&m_bBroadcast), 4  );
	inifile.write( reinterpret_cast<char*>(&m_bEtherdata), 4  );

	inifile.write( reinterpret_cast<char*>(&m_nSkipCount), 4  );	
	inifile.write( reinterpret_cast<char*>(&m_bCompass), 4  );
	inifile.write( reinterpret_cast<char*>(&m_bBigLen), 4  );
	inifile.write( reinterpret_cast<char*>(&m_bSplitBody), 4  );
	inifile.write( reinterpret_cast<char*>(&m_bWRInput), 4  );

	inifile.close();
	return TRUE;

}
BOOL	CDidsonIni::LoadDidsonIni( TCHAR* inifilename )
{
	TCHAR* strfilename;
	if( inifilename == NULL )
	{
		TCHAR Buffer[MAX_PATH];	
		GetModuleFileName(  NULL, Buffer, sizeof(Buffer) );
		int nstrpos = _tcsclen( Buffer );
		while( *(Buffer + nstrpos--) != L'\\' );	
		Buffer[nstrpos+1]	= L'\0';
		_tcscat(Buffer, L"\\DidsonV5.ini");
		strfilename = Buffer;
	}
	else
	{

		strfilename = inifilename;
	}
	//char buf[100];
	fstream inifile( strfilename, fstream::binary|fstream::in );
	if( !inifile ) return FALSE;

	inifile.read( reinterpret_cast<char*>(m_cBuffhead), 17  );
	inifile.read( reinterpret_cast<char*>(&m_nMount), 4  );

	inifile.read( reinterpret_cast<char*>(&m_nPort), 4  );
	inifile.read( reinterpret_cast<char*>(&m_nSspd), 4  );
	inifile.read( reinterpret_cast<char*>(&m_nTimeout), 4  );
	inifile.read( reinterpret_cast<char*>(&m_nSonarRecordMode), 4  );
	inifile.read( reinterpret_cast<char*>(&m_nSonarFramesPerFile), 4  );
	inifile.read( reinterpret_cast<char*>(&m_nSonarFrameRate), 4  );
	inifile.read( reinterpret_cast<char*>(m_cIPaddress), 16  );
	inifile.read( reinterpret_cast<char*>(m_cNetmask), 16  );
	inifile.read( reinterpret_cast<char*>(m_cGateway), 16  );

	inifile.read( reinterpret_cast<char*>(&m_nSN), 4  );
	inifile.read( reinterpret_cast<char*>(&m_nMclk), 4  );
	inifile.read( reinterpret_cast<char*>(&m_nWaterTemp), 4  );
	inifile.read( reinterpret_cast<char*>(&m_nSalinity), 4  );
	inifile.read( reinterpret_cast<char*>(&m_nFocusMode), 4  );
	
	inifile.read( reinterpret_cast<char*>(&m_bLongRange), 4  );
	inifile.read( reinterpret_cast<char*>(&m_bPrism), 4  );
	inifile.read( reinterpret_cast<char*>(&m_b3000m), 4  );
	inifile.read( reinterpret_cast<char*>(&m_bBroadcast), 4  );
	inifile.read( reinterpret_cast<char*>(&m_bEtherdata), 4  );
	
	inifile.read( reinterpret_cast<char*>(&m_nSkipCount), 4  );	
	inifile.read( reinterpret_cast<char*>(&m_bCompass), 4  );
	inifile.read( reinterpret_cast<char*>(&m_bBigLen), 4  );
	inifile.read( reinterpret_cast<char*>(&m_bSplitBody), 4  );
	inifile.read( reinterpret_cast<char*>(&m_bWRInput), 4  );



	inifile.close();

	return TRUE;
}

void	CDidsonIni::ToParaStruct( dds::DIDSON_SYS_PARAMS & param)
{

	param.nPort = m_nPort;
	param.nSspd = m_nSspd;				// meters/second
	param.nTimeout =m_nTimeout;				// seconds to wait for Ethernet command after power-up
	param.nSonarRecordMode = m_nSonarRecordMode;
	param.nSonarFramesPerFile = m_nSonarFramesPerFile;
	param.nSonarFrameRate = m_nSonarFrameRate;
	memcpy( param.cIPaddress, m_cIPaddress, 16 );
	memcpy( param.cNetmask, m_cNetmask, 16 );
	memcpy( param.cGateway, m_cGateway, 16 );
	param.nSN		 = m_nSN;
	param.nMclk		 = m_nMclk;				// 0=Ext windows, 1=Std windows
	param.nWaterTemp = m_nWaterTemp;			// 0=0-10C, 1=10-20C, 2=20 to 30C
	param.nSalinity	 = m_nSalinity;			// 0=fresh, 1=brackish, 2=seawater
	param.nFocusMode = m_nFocusMode;			// bit0: 0=center, 1=average
	param.bLongRange = m_bLongRange;			// .70/1.2MHz model if TRUE
	param.bPrism	 = m_bPrism;				// for Split-Body only
	param.b3000m	 = m_b3000m;				// for 3000m depth version only
	param.bBroadcast = m_bBroadcast;			// use Broadcast IP address to send data
	param.bEtherdata = m_bEtherdata;			// send Ethernet data in Autonomous mode
	param.nSkipCount = m_nSkipCount;			// send Ethernetdata every Nth frame
	param.bCompass   = m_bCompass;				// compass installed

}
void	CDidsonIni::FromParamStruct(dds::DIDSON_SYS_PARAMS  param )
{

	m_nPort=param.nPort ;
	m_nSspd=param.nSspd ;				// meters/second
	m_nTimeout=param.nTimeout;				// seconds to wait for Ethernet command after power-up
	m_nSonarRecordMode=param.nSonarRecordMode  ;
	m_nSonarFramesPerFile=param.nSonarFramesPerFile  ;
	m_nSonarFrameRate=param.nSonarFrameRate  ;
	memcpy(  m_cIPaddress,param.cIPaddress, 16 );
	memcpy(  m_cNetmask,param.cNetmask, 16 );
	memcpy(  m_cGateway,param.cGateway, 16 );
	m_nSN = param.nSN		  ;
	m_nMclk = param.nMclk		 ;				// 0=Ext windows, 1=Std windows
	m_nWaterTemp=param.nWaterTemp  ;			// 0=0-10C, 1=10-20C, 2=20 to 30C
	m_nSalinity=param.nSalinity	  ;			// 0=fresh, 1=brackish, 2=seawater
	m_nFocusMode=param.nFocusMode  ;			// bit0: 0=center, 1=average
	m_bLongRange=param.bLongRange  ;			// .70/1.2MHz model if TRUE
	m_bPrism=param.bPrism	  ;				// for Split-Body only
	m_b3000m=param.b3000m	  ;				// for 3000m depth version only
	m_bBroadcast=param.bBroadcast  ;			// use Broadcast IP address to send data
	m_bEtherdata=param.bEtherdata ;			// send Ethernet data in Autonomous mode
	m_nSkipCount=param.nSkipCount  ;			// send Ethernetdata every Nth frame
	m_bCompass=param.bCompass   ;				// compass installed


}